/*
========================================================================
 Name        : ExportContactsExportingContainer.cpp
 Author      : 
 Copyright   : Your copyright notice
 Description : 
========================================================================
*/
 
#include <barsread.h>
#include <stringloader.h>
#include <eiklabel.h>
#include <eikenv.h>
#include <gdi.h>
#include <eikgted.h>
#include <aknviewappui.h>
#include <eikappui.h>
#include <txtfrmat.h>
#include <eikrted.h>
#include <txtrich.h>
#include <gulcolor.h>
#include <ExportContacts.rsg> 
#include <aknsskininstance.h> 
#include <aknsutils.h> 
#include <aknsbasicbackgroundcontrolcontext.h> 
#include <aknscontrolcontext.h> 
#include <aknsdrawutils.h> 

#include "ExportContactsExportingContainer.h"
#include "ExportContactsExportingContainerView.h"
#include "ExportContacts.hrh"
#include "ExportContactsListBox.hrh"
#include "ExportContactsSelectDirContainer.hrh"
#include "ExportContactsExportingContainer.hrh"
 

_LIT(KSplash,"\/");
_LIT(KPercent,"%");

CExportContactsExportingContainer::CExportContactsExportingContainer()
{	  
	iExportingProcessTitleLabel = NULL;
	iMessageNoteRichText = NULL;	
	iExportFile = NULL;
	iWholeCount = 1;
	iExportingPercent = 0.0;
}
 
CExportContactsExportingContainer::~CExportContactsExportingContainer()
{		
	if(iExportingProcessTitleLabel)
	{
		delete iExportingProcessTitleLabel;
		iExportingProcessTitleLabel = NULL;
	}	
	if(iMessageNoteRichText)
	{
		delete iMessageNoteRichText;	
		iMessageNoteRichText = NULL;	
	}	 
	if(iCharform)
	{
		delete iCharform;
		iCharform = NULL;
	}	
	if(iParaform)
	{
		delete iParaform;
		iParaform = NULL;
	}	
	if(iParaFormat)
	{
		delete iParaFormat;
		iParaFormat = NULL;
	}
	
	if(iRichMessage)
	{
		delete iRichMessage;
		iRichMessage = NULL;
	}		
	if(iBgContext)
	{
		delete iBgContext;
		iBgContext = NULL;
	}	
	if(iExportFile)
	{
		delete iExportFile;
		iExportFile = NULL;
	}
}				
 
CExportContactsExportingContainer* CExportContactsExportingContainer::NewL( 
																			const TRect& aRect, 
																			const CCoeControl* aParent, 
																			MEikCommandObserver* aCommandObserver )
{
	CExportContactsExportingContainer* self = CExportContactsExportingContainer::NewLC( 
																						aRect, 
																						aParent, 
																						aCommandObserver );
	CleanupStack::Pop( self );
	return self;
}
 
CExportContactsExportingContainer* CExportContactsExportingContainer::NewLC( 
																			const TRect& aRect, 
																			const CCoeControl* aParent, 
																			MEikCommandObserver* aCommandObserver )
{
	CExportContactsExportingContainer* self = new ( ELeave ) CExportContactsExportingContainer();
	CleanupStack::PushL( self );
	self->ConstructL( aRect, aParent, aCommandObserver );
	return self;
}
  
void CExportContactsExportingContainer::ConstructL( 
													const TRect& aRect, 
													const CCoeControl* aParent, 
													MEikCommandObserver* aCommandObserver )
{
	if ( aParent == NULL )
	{
		CreateWindowL();
	}
	else
	{
	    SetContainerWindowL( *aParent );
	}
	iBgContext = CAknsBasicBackgroundControlContext::NewL( KAknsIIDQsnBgAreaMain,aRect,ETrue);
	iFocusControl = NULL;
	iCommandObserver = aCommandObserver;
	InitializeControlsL();
	SetRect( aRect );
	ActivateL();	 
	SetMessageNoteRichTextFormattingL();	
}			
 
TInt CExportContactsExportingContainer::CountComponentControls() const
{
	return ( int ) ELastControl;
}				
 
CCoeControl* CExportContactsExportingContainer::ComponentControl( TInt aIndex ) const
{ 
	switch ( aIndex )
	{ 
		case EExportingProcessTitleLabel:
			return iExportingProcessTitleLabel;
		case EMessageNoteRichText:
			return iMessageNoteRichText;
	}	
	return NULL;
}				
 			
void CExportContactsExportingContainer::SizeChanged()
{
	CCoeControl::SizeChanged();
	LayoutControls(); 
	if(iBgContext)
	{
		iBgContext->SetRect(Rect());
		if ( &Window() )
		{
			iBgContext->SetParentPos( PositionRelativeToScreen() );
		}
	}
}		

void CExportContactsExportingContainer::SetExportFile(const TDesC& aExportFile)
{
	if(iExportFile)
	{
		delete iExportFile;
		iExportFile = NULL;
	}
	iExportFile = HBufC::NewL(aExportFile.Length());
	TPtr ptr(iExportFile->Des());
	
	ptr.Append(aExportFile);
	
	if(iRichMessage)
	{
		delete iRichMessage;
	}
	
	iRichMessage = CRichText::NewL( iParaform, iCharform );
	HBufC* text = StringLoader::LoadLC( R_EXPORT_CONTACTS_EXPORTING_CONTAINER_MESSAGE_NOTE_RICH_TEXT_2 );
    iMessageNoteRichText->SetTextL( text );
	iRichMessage->InsertL(0,*text);
	TInt pLength = iRichMessage->LdDocumentLength();	
	iRichMessage->InsertL(pLength,aExportFile);
	iMessageNoteRichText->SetDocumentContentL( *iRichMessage );
	CleanupStack::PopAndDestroy( text );	
}

void CExportContactsExportingContainer::ExportingNotify(const TInt aCurrentCount,const TInt aWholeCount)
{
	iCurrentCount = aCurrentCount;
	iWholeCount = aWholeCount;
	iExportingPercent = ((iCurrentCount+1)/(iWholeCount*1.0));
	ChangeNotifyText();
	DrawNow();
}

TTypeUid::Ptr CExportContactsExportingContainer::MopSupplyObject(TTypeUid aId)
{
    if (iBgContext )
    {
        return MAknsControlContext::SupplyMopObject( aId, iBgContext );
    }
    return CCoeControl::MopSupplyObject(aId);
}

void CExportContactsExportingContainer::LayoutControls()
{
	TRect pRect(Rect()); 
	iExportingProcessTitleLabel->SetExtent( TPoint( 0, pRect.Height()*0.35 ), TSize( pRect.Width(), pRect.Height()*0.2 ) );
	iMessageNoteRichText->SetExtent( TPoint( 0, pRect.Height()*0.55 ), TSize( pRect.Width(), pRect.Height()*0.5 ) );
}
 			
TKeyResponse CExportContactsExportingContainer::OfferKeyEventL( 
																const TKeyEvent& aKeyEvent, 
																TEventCode aType )
{	
	if ( iFocusControl != NULL
		&& iFocusControl->OfferKeyEventL( aKeyEvent, aType ) == EKeyWasConsumed )
	{
		return EKeyWasConsumed;
	}
	return CCoeControl::OfferKeyEventL( aKeyEvent, aType );
}
				
 				
void CExportContactsExportingContainer::InitializeControlsL()
{ 
	iExportingProcessTitleLabel = new ( ELeave ) CEikLabel;
	iExportingProcessTitleLabel->SetContainerWindowL( *this );
	{
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_EXPORT_CONTACTS_EXPORTING_CONTAINER_EXPORTING_PROCESS_TITLE_LABEL );
		iExportingProcessTitleLabel->ConstructFromResourceL( reader );
		CleanupStack::PopAndDestroy();  
	}
	iMessageNoteRichText = new ( ELeave ) CEikRichTextEditor;
	iMessageNoteRichText->SetContainerWindowL( *this );
	{
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_EXPORT_CONTACTS_EXPORTING_CONTAINER_MESSAGE_NOTE_RICH_TEXT );
		iMessageNoteRichText->ConstructFromResourceL( reader );
		CleanupStack::PopAndDestroy();  
	}	 
}
 
void CExportContactsExportingContainer::HandleResourceChange( TInt aType )
{
	CCoeControl::HandleResourceChange( aType );
	SetRect( iAvkonViewAppUi->View( TUid::Uid( EExportContactsExportingContainerViewId ) )->ClientRect() );
}
				
 			
void CExportContactsExportingContainer::Draw( const TRect& aRect ) const
{	 
	TRect pRect(aRect);
	CWindowGc& gc = SystemGc();
	gc.Clear( pRect );
	
	MAknsSkinInstance* skin = AknsUtils::SkinInstance();
	MAknsControlContext* cc = AknsDrawUtils::ControlContext( this );
	AknsDrawUtils::Background( skin, cc, this, gc,aRect);
	
//	TReal percent =((iCurrentCount+1)/(iWholeCount*1.0));
	
	
	gc.SetBrushStyle( CGraphicsContext::ESolidBrush );	
	
	TRect pWholeRect(TPoint( 10, pRect.Height()*0.05 ), TPoint( pRect.Width()-10, pRect.Height()*0.15 ) );
	gc.SetBrushColor(KRgbWhite); 
	gc.DrawRect(pWholeRect);
	
	gc.SetBrushColor(KRgbGreen); 
	gc.SetPenStyle( CGraphicsContext::ESolidPen );
	gc.SetPenColor(KRgbGreen);
	TRect pProcessRect(TPoint( 10, pRect.Height()*0.05  ), 
					   TPoint( (pRect.Width()-10)*iExportingPercent, pRect.Height()*0.15 ) );
	gc.DrawRect(pProcessRect);
	
	const CFont* font = iEikonEnv->LegendFont();
	gc.UseFont(font);	 
	gc.SetPenColor(KRgbBlack);
	HBufC* pExportingInfo = HBufC::NewL(100);
	TPtr ptr(pExportingInfo->Des()); 
	
	ptr.AppendNum(iCurrentCount+1);
	ptr.Append(KSplash);
	ptr.AppendNum(iWholeCount);
	
	TPoint pTextPoint;
	pTextPoint.iX = 10;
	pTextPoint.iY = pRect.Height()*0.15 + font->HeightInPixels();
    
	gc.DrawText(ptr,pTextPoint);
	
	ptr.Zero();
	HBufC* pAlreadyComplete = StringLoader::LoadL(R_EXPORT_CONTACTS_EXPORTING_CONTAINER_AREADYCOMPLETE);
	ptr.Append(*pAlreadyComplete);
	ptr.AppendNum(iExportingPercent*100);
	ptr.Append(KPercent);
	
	pTextPoint.iX = pRect.Width()-10 - font->TextWidthInPixels(ptr);
	gc.DrawText(ptr,pTextPoint);
	
	delete pAlreadyComplete;
	delete pExportingInfo;
	gc.DiscardFont();
 
}				
void CExportContactsExportingContainer::ChangeNotifyText()
{
	TReal percent = (iCurrentCount+1)/(iWholeCount*1.0);
	if(percent>=1)
	{
		if(iRichMessage)
		{
			delete iRichMessage;
			iRichMessage = NULL;
		}
		{
			iRichMessage = CRichText::NewL( iParaform, iCharform );		 
			HBufC* text = StringLoader::LoadLC( R_EXPORT_CONTACTS_EXPORTING_CONTAINER_SUCCESSTOEXPORT );
			if(text)
			{
				if(iExportFile)
				{
					HBufC* message = HBufC::NewL(text->Length()+iExportFile->Length());
					TPtr  ptr(message->Des());
					ptr.Append(*text);
					ptr.Append(*iExportFile);
					
					iMessageNoteRichText->SetTextL( message );
					iRichMessage->InsertL(0,*message);
					iMessageNoteRichText->SetDocumentContentL( *iRichMessage );
					delete message;
				}			
			}		
			CleanupStack::PopAndDestroy( text );	
		}
		{
		HBufC* text = StringLoader::LoadLC( R_EXPORT_CONTACTS_EXPORTING_CONTAINER_SUCCESS );		
		iExportingProcessTitleLabel->SetTextL(*text);
		iExportingProcessTitleLabel->DrawNow();
		CleanupStack::PopAndDestroy( text );	
		}
	
	}
}
void CExportContactsExportingContainer::SetMessageNoteRichTextFormattingL()
{
	iCharform = CEikonEnv::NewDefaultCharFormatLayerL(); 
	TCharFormat charFormat; 
	charFormat.iFontPresentation.iTextColor = KRgbBlack;
	TCharFormatMask charFormatMask;
	charFormatMask.SetAttrib(EAttColor);

	iCharform->Sense( charFormat, charFormatMask ); 
	iCharform->SetL( charFormat, charFormatMask );

	iParaform = CEikonEnv::NewDefaultParaFormatLayerL(); 
	iParaFormat = new( ELeave ) CParaFormat();
	TParaFormatMask paraFormatMask;
	iParaform->SenseL( iParaFormat, paraFormatMask ); 
	iParaform->SetL( iParaFormat, paraFormatMask );

	iRichMessage = CRichText::NewL( iParaform, iCharform );
	HBufC* text = StringLoader::LoadLC( R_EXPORT_CONTACTS_EXPORTING_CONTAINER_MESSAGE_NOTE_RICH_TEXT_2 );
	iMessageNoteRichText->SetTextL( text );
	iRichMessage->InsertL(0,*text);
	iMessageNoteRichText->SetDocumentContentL( *iRichMessage );
	CleanupStack::PopAndDestroy( text );	
}
